package com.bignerdranch.android.sensorapplication;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;
import java.util.List;

public class DataAnalysisActivity extends AppCompatActivity implements View.OnClickListener {

    private Button showAccImage;
    private Button showGyroImage;
    private Button showPressureImage;
    private Button showMagneticImage;
    private String getPath;
    private LineChart mLineChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_analysis);
        getPath = getIntent().getStringExtra("path");
        initViews();
    }

    private void initViews() {
        mLineChart = findViewById(R.id.data_line_chart);
        showAccImage = findViewById(R.id.acc_image);
        showGyroImage = findViewById(R.id.gyro_image);
        showPressureImage = findViewById(R.id.pressure_image);
        showMagneticImage = findViewById(R.id.magnetic_image);
        showAccImage.setOnClickListener(this);
        showGyroImage.setOnClickListener(this);
        showPressureImage.setOnClickListener(this);
        showMagneticImage.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.acc_image:
                getPath = getPath + "/acceleration.txt";
                handleThreeLineText(getPath);
                break;
            case R.id.pressure_image:
                getPath = getPath + "/pressure.txt";
                handleOneLineText(getPath);
                break;
            case R.id.magnetic_image:
                getPath = getPath + "/magnetic_magnitude.txt";
                handleOneLineText(getPath);
                break;
            case R.id.gyro_image:
                getPath = getPath + "/gyroscope.txt";
                handleThreeLineText(getPath);
                break;
        }
    }

    private void handleThreeLineText(String path) {
        initChart(mLineChart);
        ArrayList<Entry> dataListX = new ArrayList<>();
        ArrayList<Entry> dataListY = new ArrayList<>();
        ArrayList<Entry> dataListZ = new ArrayList<>();
        String getContent = FileOperator.readFile(path);
        String[] content = getContent.split("\n");
        for (int i = 0; i < content.length; i++) {
            String[] details = content[i].split(",");
            dataListX.add(new Entry(i, Float.parseFloat(details[0])));
            dataListY.add(new Entry(i, Float.parseFloat(details[1])));
            dataListZ.add(new Entry(i, Float.parseFloat(details[2])));
        }
        showLineChart(dataListX, "x_line", Color.BLACK);
        addLine(dataListY, "y_line", Color.BLUE);
        addLine(dataListZ, "z_line", Color.GREEN);
        Legend l = mLineChart.getLegend();
        l.setForm(Legend.LegendForm.LINE);
    }

    private void handleOneLineText(String path) {
        initChart(mLineChart);
        ArrayList<Entry> dataList = new ArrayList<>();
        String getContent = FileOperator.readFile(path);
        String[] content = getContent.split("\n");
        for (int i = 0; i < content.length; i++) {
            String[] details = content[i].split(",");
            dataList.add(new Entry(i, Float.parseFloat(details[0])));
        }
        showLineChart(dataList, "single_line", Color.RED);
    }

    private void initChart(LineChart lineChart) {
        lineChart.setDrawGridBackground(false);
        lineChart.setDrawBorders(true);
        lineChart.setDragEnabled(false);
        lineChart.setTouchEnabled(true);
        //y轴设置
        YAxis left = lineChart.getAxisLeft();
        YAxis right = lineChart.getAxisRight();
        right.setEnabled(false);
        left.setDrawGridLines(false);
//        left.setAxisMinimum(0f);
    }

    private void showLineChart(List<Entry> dataList, String name, int color) {
        LineDataSet lineDataSet = new LineDataSet(dataList, name);
        initLineDataSet(lineDataSet, color);
        LineData lineData = new LineData(lineDataSet);
        mLineChart.setData(lineData);
    }

    private void initLineDataSet(LineDataSet lineDataSet, int color) {
        lineDataSet.setColor(color);
        lineDataSet.setCircleColor(color);
        lineDataSet.setLineWidth(1f);
        lineDataSet.enableDashedLine(10f, 5f, 0f);
        lineDataSet.enableDashedHighlightLine(10f, 5f, 0f);
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setDrawCircles(false);
        lineDataSet.setDrawValues(false);
        lineDataSet.setFormLineWidth(1f);
        lineDataSet.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
        lineDataSet.setFormSize(15f);
        lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
    }

    private void addLine(List<Entry> dataList, String name, int color) {
        LineDataSet lineDataSet = new LineDataSet(dataList, name);
        initLineDataSet(lineDataSet, color);
        mLineChart.getLineData().addDataSet(lineDataSet);
        mLineChart.invalidate();
    }
}
